package com.ruantu.clothing.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruantu.clothing.entity.JsonResponseMessage;
import com.ruantu.clothing.entity.Permission;
import com.ruantu.clothing.entity.User;
import com.ruantu.clothing.service.PermissionService;

@Controller
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    private static final Logger logger = LoggerFactory.getLogger(PermissionController.class);

    @RequestMapping("/permission")
    @ResponseBody
    public List<Permission> getAllPermission() {

        return permissionService.findAll();

    }


    @RequestMapping("/getOneRolePer")
    @ResponseBody
    public JsonResponseMessage getOneRolePer(int roleid, HttpSession session) {
        JsonResponseMessage msg = new JsonResponseMessage();
        User u = (User) session.getAttribute("info");
        try {
            Map<String, Object> map = permissionService.getOneRolePer(roleid);
            if (map != null) {
                msg.setCode(0);
                msg.setData(map);
                logger.info("查询权限成功，执行人：" + u.getUserName());
            } else {
                msg.setCode(1);
                logger.info("查询权限失败，执行人：" + u.getUserName());
            }
        } catch (Exception e) {

            logger.error("查询权限异常，执行人:" + u.getUserName(), e.getStackTrace());

            msg.setCode(1);

        }

        return msg;
    }

}
